﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Basicas;
using ProjetoJornada_Negocios.Fachada;
using Basicas.Utils.excecoes;
using Basicas.Utils;

namespace ProjetoJornada.Interno
{
    public partial class Jornada_Editar : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string queryString = Request.QueryString["id"];
            int id = Convert.ToInt32(queryString);

            if (!IsPostBack)
            {
                Jornada jornada = Fachada.Instance.ConsultarJornadaPorId(id);
                
                if (jornada != null)
                {
                    this.txtNomeJornada.Text = jornada.Nome;
                    this.txtDataInicio.Text = jornada.DataInicial.ToString("dd/MM/yyyy");
                    this.txtDataFim.Text = jornada.DataFinal.ToString("dd/MM/yyyy");
                    this.txtNomeResponsavel.Text = jornada.Responsavel.Nome;
                    this.txtTelefone.Text = jornada.Responsavel.Telefone;
                    this.txtEmail.Text = jornada.Responsavel.Email;
                    this.txtCelular.Text = jornada.Responsavel.Celular;
                    this.txtCPF.Text = jornada.Responsavel.Cpf;

                    int cargo = (int)jornada.Responsavel.Cargo;
                    
                    string cargoString = cargo.ToString();
                    if (cargoString.Length == 2)
                    {
                        this.CargoDropDownList.SelectedValue = cargoString.Substring(0, 1);
                        this.CargoDropDownList_SelectedIndexChanged(this.CargoDropDownList, new EventArgs());
                        this.NucleoDropDownList.SelectedValue = cargoString.Substring(1, 1);
                    }

                    ViewState["Jornada"] = jornada;
                }
            }
        }

        protected void CargoDropDownList_SelectedIndexChanged(object sender, EventArgs e)
        {

            if (this.CargoDropDownList.SelectedIndex == 1)
            {
                ListItem[] nucleos = { new ListItem("Comercial", "1"), new ListItem("Financeiro", "2"), new ListItem("de Marketing", "3"), new ListItem("de Projetos", "4"), new ListItem("de Qualidade", "5"), new ListItem("de Recursos Humanos", "6"), new ListItem("Presidente Executivo", "7"), new ListItem("Presidente Adiministrativo", "8") };
                this.NucleoDropDownList.Items.Clear();
                this.NucleoDropDownList.Items.AddRange(nucleos);
            }
            else if (this.CargoDropDownList.SelectedIndex == 2)
            {

                ListItem[] nucleos = { new ListItem("Comercial", "1"), new ListItem("Financeiro", "2"), new ListItem("de Marketing", "3"), new ListItem("de Projetos", "4"), new ListItem("de Qualidade", "5"), new ListItem("de Recursos Humanos", "6") };
                this.NucleoDropDownList.Items.Clear();
                this.NucleoDropDownList.Items.AddRange(nucleos);
            }

        }

        protected void ibtnEditar_Click(object sender, ImageClickEventArgs e)
        {
            Jornada jornada = ViewState["Jornada"] as Jornada;
            if (jornada != null)
            {
                try
                {
                    jornada.Nome = this.txtNomeJornada.Text;
                    jornada.DataInicial = DateTime.Parse(this.txtDataInicio.Text);
                    jornada.DataFinal = DateTime.Parse(this.txtDataFim.Text);
                    jornada.Responsavel.Nome = this.txtNomeResponsavel.Text;
                    jornada.Responsavel.Telefone = this.txtTelefone.Text;
                    jornada.Responsavel.Email = this.txtEmail.Text;
                    jornada.Responsavel.Celular = this.txtCelular.Text;
                    string cpf = this.txtCPF.Text;
                    cpf = cpf.Replace(".", "");
                    cpf = cpf.Replace("-", "");
                    jornada.Responsavel.Cpf = cpf;                    

                    int cargo = 0;
                    if (CargoDropDownList.SelectedValue != "" && NucleoDropDownList.SelectedValue != "")
                    {
                        cargo = Convert.ToInt32(String.Concat(CargoDropDownList.SelectedValue, NucleoDropDownList.SelectedValue));
                    }
                    CargoCiti cc = (CargoCiti)cargo;

                    jornada.Responsavel.Cargo = cc;

                    Fachada.Instance.EditarJornada(jornada);

                    Response.Redirect("Jornada_Listar.aspx");
                }
                catch (PeriodoInvalidoException)
                {
                    this.lblMensagemErro.Text = MensagensPadrao.ERRO_PERIODO_INVALIDO;
                }
            }
        }
    }
}
